package com.llsly.wearabledataviz.controller;

import com.llsly.wearabledataviz.model.WearableData;
import com.llsly.wearabledataviz.service.WearableDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/wearable")
@CrossOrigin(origins = "*")
public class WearableDataController {

    @Autowired
    private WearableDataService service;

    @PostMapping("/data")
    public ResponseEntity<Void> saveData(@RequestBody WearableData data) {
        service.saveData(data);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/data")
    public ResponseEntity<List<WearableData>> getData(
            @RequestParam String deviceId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end) {
        return ResponseEntity.ok(service.getDataByTimeRange(deviceId, start, end));
    }

    @GetMapping("/anomalies")
    public ResponseEntity<List<WearableData>> getAnomalies(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end) {
        return ResponseEntity.ok(service.getAnomalies(start, end));
    }
} 